<?php

namespace app\api\model\data\currency;

use app\api\model\dao\UserCurrency;
use app\api\model\dao\UserCurrencyExchange;
use think\Db;

class ConsumeCurrency
{
    private $objCurrency;
    private $objExchange;

    public function __construct()
    {
        $this->objCurrency = new UserCurrency();
        $this->objExchange = new UserCurrencyExchange();
    }

    /**
     * @param $increase_user_id //被打赏人的userid
     * @param $decrease_user_id //打赏人用户userid
     * @param $amount //数量
     * @param $currency_type //1漫币，2漫豆
     * @param $exchange_type //消费类型：1：打赏，2,：购物，3：购买漫币，4：购买漫豆
     * @return bool
     */
    public function consumeCurrency($increase_user_id, $decrease_user_id, $amount, $currency_type, $exchange_type)
    {
        Db::startTrans();
        try {
            //打赏人减少漫币或者漫豆  被打赏人减少漫币或者漫豆
            $this->objCurrency->changeCurrency($decrease_user_id, $currency_type, $amount, 0);
            $this->objCurrency->changeCurrency($increase_user_id, $currency_type, $amount, 1);

            //增加打赏人，被打赏人的消费记录
            unset($data);
            $data = [
                [
                    'user_id' => $increase_user_id,
                    'currency_type' => $currency_type,
                    'amount' => $amount,
                    'exchange_type' => $exchange_type,
                ],
                [
                    'user_id' => $decrease_user_id,
                    'currency_type' => $currency_type,
                    'amount' => $amount - 2 * $amount,
                    'exchange_type' => $exchange_type,
                ],
            ];
            $this->objExchange->addExchangeRecord($data);

            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            \think\Log::error(__METHOD__ . "change user currency failed: $increase_user_id,$decrease_user_id,$amount,$currency_type,$exchange_type");
            Db::rollback();
            return false;
        }

    }

    /**
     * @param $user_id
     * @param $amount
     * @param $currency_type
     * @param $exchange_type
     * @param $type //1增大，0减小
     * @return bool
     */
    public function addCurrency($user_id, $amount, $currency_type, $exchange_type, $type = 1)
    {
        Db::startTrans();
        try {
            //用户更改账号漫币或者漫豆数量
            $this->objCurrency->changeCurrency($user_id, $currency_type, $amount, $type);
            //增加记录
            unset($data);
            $data = [
                [
                    'user_id' => $user_id,
                    'currency_type' => $currency_type,
                    'amount' => $amount,
                    'exchange_type' => $exchange_type,
                    'type' => $type,
                ],
            ];
            $this->objExchange->addExchangeRecord($data);
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            \think\Log::error(__METHOD__ . "change user currency failed: $user_id,$user_id,$amount,$currency_type,$exchange_type: " . $e->getMessage());
            Db::rollback();
            return false;
        }

    }


}